package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.vo.OrderPayment;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("order")
public class OrderController {

    Logger logger = LoggerFactory.getLogger(OrderController.class);
    @DubboReference
    private OrderService orderService;
    /*商品订单创建*/
    @RequestMapping("generateOrder")
    @ResponseBody
    public AjaxResult generateOrder(TbOrder order){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (order == null){
            return AjaxResult.ERROR();
        }
        try {
            OrderPayment orders = orderService.addOrder(order,user);
            if (orders != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(orders);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("用户购物车购买记录失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
}
